// pages/user/user.js
const {getCollectMovie,getHistoryMovie} = require("../../models/user")
Page({
  data: {
    isAuth:false,
    logo:"",
    username:"",
    movies:""
  },
  onLoad:async function (options) {
    wx.getSetting({
      success:(res)=>{
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: (res)=> {
              var {avatarUrl,nickName} = res.userInfo
              // this 指向性问题，改为箭头函数
             
              this.setData({
                isAuth:true,
                logo:avatarUrl,
                username:nickName
              })
              this.getDefaultHttp()
            }
          })
        }
      }
    })
  },
  bindGetUserInfo (e) {
    var userInfo = e.detail.userInfo
    if(userInfo){
      var {avatarUrl,nickName} = userInfo
      this.setData({
        isAuth:true,
        logo:avatarUrl,
        username:nickName
      })
    }
  },
  async onChange(event){
    var name = event.detail.name
    var result
    if(name=='getCollect'){
      var res = await getCollectMovie()
      result = res.data.res
    }else{
      var res = await getHistoryMovie()
      result = res.data.res
    }
    this.setData({
      movies:result
    })
  },
  async getDefaultHttp(){
    var res = await getCollectMovie()
    var  result = res.data.res
    this.setData({
      movies:result
    })
  }
})